package com.chuantou.yuba.ui.widget;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;

import com.chuantou.yuba.R;
import com.chuantou.yuba.api.to.TrendsCommentTO;
import com.chuantou.yuba.api.to.TrendsTO;
import com.chuantou.yuba.ui.fragment.TrendsFragment;
import com.chuantou.yuba.util.ProfileLinkMovementMethod;
import com.chuantou.yuba.util.ProfileSpan;
import com.easemob.applib.utils.SmileUtils;

public class TrendsCommentLayout extends LinearLayout {
	public List<TrendsCommentTO> mComments;
	private ViewRecycler<TrendsCommentView> mRecycler;
	private TrendsFragment mFragment;
	private TrendsTO mTrends;
	private View mIndicatorView;

	public TrendsCommentLayout(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
	}

	public TrendsCommentLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public TrendsCommentLayout(Context context) {
		super(context);
	}

	public void setViewRecycler(ViewRecycler<TrendsCommentView> recycler) {
		this.mRecycler = recycler;
	}

	public void setTrendsFragment(TrendsFragment fragment) {
		this.mFragment = fragment;
	}

	public void setTrendsTO(TrendsTO trendsTO) {
		this.mTrends = trendsTO;
	}

	public TrendsTO getTrendsTO() {
		return mTrends;
	}

	public void setIndicatorView(View v) {
		this.mIndicatorView = v;
	}

	public void setComments(List<TrendsCommentTO> comments, int totalCount) {
		this.mComments = comments;
		if (mRecycler == null) {
			mRecycler = new ViewRecycler<TrendsCommentView>();
		}
		mRecycler.fillScrapViews(this);
		removeAllViews();
		if (mComments == null || mComments.isEmpty()) {
			if (mIndicatorView != null) {
				mIndicatorView.setVisibility(View.GONE);
			}
			return;
		}
		if (mIndicatorView != null) {
			if (mComments.size() < totalCount) {
				mIndicatorView.setVisibility(View.VISIBLE);
			} else {
				mIndicatorView.setVisibility(View.GONE);
			}
		}
		for (int i = 0; i < comments.size(); i++) {
			TrendsCommentTO comment = comments.get(i);
			TrendsCommentView textView = getCommentTextView();
			textView.setComment(comment);
			textView.setTrends(mTrends);
			textView.setTrendsFragment(mFragment);
			if (mFragment != null) {
				mFragment.registerForContextMenu(textView);
			}
			SpannableStringBuilder builder = new SpannableStringBuilder();
			String nickname = comment.getNickname();
			SpannableStringBuilder commentBuilder = new SpannableStringBuilder(nickname);
			ProfileSpan commentSpan = new ProfileSpan(nickname, comment.getUid());
			commentBuilder.setSpan(commentSpan, 0, nickname.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			builder.append(commentBuilder);
			if (!TextUtils.isEmpty(comment.getReplyNickname()) && comment.getReplyUid() > 0) {
				builder.append(getContext().getString(R.string.reply));
				String replyNickname = comment.getReplyNickname();
				SpannableStringBuilder replyBuilder = new SpannableStringBuilder(replyNickname);
				ProfileSpan replySpan = new ProfileSpan(replyNickname, comment.getReplyUid());
				replyBuilder.setSpan(replySpan, 0, replyNickname.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
				builder.append(replyBuilder);
			}
			builder.append(":");
			Spannable content = SmileUtils.getSmiledText(getContext(), comment.getContent());
			builder.append(content);
			textView.setText(builder);
			builder.append(comment.getContent());
			addViewInLayout(textView, i, generateDefaultLayoutParams(), true);
		}
	}

	private TrendsCommentView getCommentTextView() {
		TrendsCommentView view = mRecycler.getScrapView();
		if (view == null) {
			view = new TrendsCommentView(getContext());
		}
		return view;
	}
}
